package com.hhf.riceshopping.seckill.controller;

import com.hhf.common.utils.R;
import com.hhf.riceshopping.seckill.service.SeckillService;
import com.hhf.riceshopping.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    //获取当前时间需要参与的秒杀的商品
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
       List<SecKillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    //获取当期秒杀的sku
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getskuSeckillInfo(@PathVariable("skuId") Long skuId){
        SecKillSkuRedisTo to = seckillService.getskuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    //秒杀功能
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                     @RequestParam("key") String key,
                     @RequestParam("num") Integer num,
                     Model model){
        String orderSn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }

}
